package com.lyzbbb.demo.miniapp.config;

import com.lyzbbb.demo.miniapp.entity.Student;
import com.lyzbbb.demo.miniapp.repository.StudentRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;

import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * 测试数据初始化
 */
@Component
public class DataInitializer implements CommandLineRunner {

    private final StudentRepository studentRepository;
    private final SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");

    @Autowired
    public DataInitializer(StudentRepository studentRepository) {
        this.studentRepository = studentRepository;
    }

    @Override
    public void run(String... args) throws Exception {
        // 清除所有现有数据
        studentRepository.deleteAll();
        
        System.out.println("已删除所有学生数据，开始初始化测试数据...");
            
        // 创建并保存一些测试学生数据
        Student student1 = new Student("202001001", "张三");
        student1.setBirthDate(dateFormat.parse("2000-01-15"));
        studentRepository.save(student1);
        System.out.println("已创建学生: " + student1.toString());
        
        Student student2 = new Student("202001002", "李四");
        student2.setBirthDate(dateFormat.parse("2000-05-20"));
        studentRepository.save(student2);
        System.out.println("已创建学生: " + student2.toString());
        
        Student student3 = new Student("202001003", "王五");
        student3.setBirthDate(dateFormat.parse("2000-09-10"));
        studentRepository.save(student3);
        System.out.println("已创建学生: " + student3.toString());
        
        Student student4 = new Student("202001004", "赵六");
        student4.setBirthDate(dateFormat.parse("2001-03-25"));
        studentRepository.save(student4);
        System.out.println("已创建学生: " + student4.toString());
        
        Student student5 = new Student("202001005", "钱七");
        student5.setBirthDate(dateFormat.parse("2001-11-05"));
        studentRepository.save(student5);
        System.out.println("已创建学生: " + student5.toString());
        
        System.out.println("测试数据初始化完成！");
    }
} 